package com.wnxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wnxy.dto.DeleteDto;
import com.wnxy.dto.SeatDto;
import com.wnxy.dto.StatusCode;
import com.wnxy.exception.DeleteException;
import com.wnxy.exception.OrderException;
import com.wnxy.exception.QueryException;
import com.wnxy.model.Perform;
import com.wnxy.model.Seat;
import com.wnxy.mapper.SeatMapper;
import com.wnxy.model.Ticket;
import com.wnxy.service.PerformService;
import com.wnxy.service.RoomService;
import com.wnxy.service.SeatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.util.BeanCopyUtil;
import com.wnxy.vo.SeatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author alice
 * @since 2021-10-25
 */
@Service
public class SeatServiceImpl extends ServiceImpl<SeatMapper, Seat> implements SeatService {
    @Autowired
    private RoomService roomService;
    @Autowired
    private PerformService performService;
    @Autowired
    private StringRedisTemplate srt;
    @Autowired
    private RedisTemplate<String, Object> rt;

    @Override
    public List<SeatDto> seatList(Integer roomId) {
        List<Seat> seats = list(new QueryWrapper<Seat>().eq("seat_room_id", roomId).orderByAsc("seat_line").orderByAsc("seat_row"));

        if(seats==null) throw new QueryException(StatusCode.ERROR.getCode(), "查询失败");

        return BeanCopyUtil.copyList(seats, SeatDto.class);
    }
    @Override
    public List<SeatDto> seatListByPerform(Integer performId) {
        Perform perform = performService.getById(performId);
        List<Seat> seats = list(new QueryWrapper<Seat>().eq("seat_room_id", perform.getPerformRoomId()).orderByAsc("seat_line").orderByAsc("seat_row"));
        List<SeatDto> seatDtos = BeanCopyUtil.copyList(seats, SeatDto.class);
        List<Object> ticketsObj = rt.opsForHash().values("wnxy:theater:ticket:" + performId);
        List<Ticket> tickets = BeanCopyUtil.copyList(ticketsObj, Ticket.class);
        System.out.println(tickets);
        for (SeatDto seat : seatDtos) {
            for (Ticket ticket : tickets) {
                if(ticket.getTicketSeatId()==seat.getSeatId()){
                    seat.setTicket(ticket);
                }
            }
        }
        return seatDtos;
    }
    @Override
    public DeleteDto banSeat(SeatVo seatVo) {
        List<Integer> ids = seatVo.getSeatIds();
        List<Seat> seats = list(new QueryWrapper<Seat>().in("seat_id", seatVo.getSeatIds()));
//        List<Seat> seats = listByIds(seatVo.getSeatIds());
        System.out.println(seats);
        ArrayList<Integer> list = new ArrayList<>();
        seats.forEach(seat->{
            list.add(seat.getSeatId());//数组保存可以进行删除的id
            seat.setSeatStatus(1);
        });
        ids.removeAll(list);//不能删除的ids
        //.改变座位状态
        boolean b = updateBatchById(seats);
        System.out.println(seats);
        if(!b) throw new DeleteException(StatusCode.DELETEERROR.getCode(), "删除失败");
//        if(!remove) throw new DeleteException(StatusCode.DELETEERROR.getCode(), "删除失败");
        DeleteDto deleteDto = new DeleteDto();
        deleteDto.setDelIds(list);
        deleteDto.setNotDelIds(ids);
        return deleteDto;
    }
    @Override
    public DeleteDto upSeat(SeatVo seatVo) {
        List<Integer> ids = seatVo.getSeatIds();
        List<Seat> seats = list(new QueryWrapper<Seat>().in("seat_id", seatVo.getSeatIds()));
//        List<Seat> seats = listByIds(seatVo.getSeatIds());
        System.out.println(seats);
        ArrayList<Integer> list = new ArrayList<>();
        seats.forEach(seat->{
            list.add(seat.getSeatId());//数组保存可以进行删除的id
            seat.setSeatStatus(0);
        });
        ids.removeAll(list);//不能删除的ids
        //.改变座位状态
        boolean b = updateBatchById(seats);
        System.out.println(seats);
        if(!b) throw new DeleteException(StatusCode.DELETEERROR.getCode(), "上架失败");
//        if(!remove) throw new DeleteException(StatusCode.DELETEERROR.getCode(), "删除失败");
        DeleteDto deleteDto = new DeleteDto();
        deleteDto.setDelIds(list);
        deleteDto.setNotDelIds(ids);
        return deleteDto;
    }
    @Override
    public List<SeatDto> seatsByPerform(Integer performId) {
        List<Object> ticketsObj = rt.opsForHash().values("wnxy:theater:ticket:" + performId);
        if(ticketsObj==null) throw new OrderException(StatusCode.ORDERERROR.getCode(), "演出信息不存在");
        List<Ticket> tickets = BeanCopyUtil.copyList(ticketsObj, Ticket.class);
//        List<Seat> seats = list(new QueryWrapper<Seat>().eq("seat_room_id", roomId).orderByAsc("seat_line").orderByAsc("seat_row"));

        return null;
    }


}
